package com.hxexp.weixin.hpe.api.orderdetail.impl;

import java.io.IOException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.type.TypeReference;
import com.hxexp.weixin.common.config.HpeApiURLConfig;
import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.httpclient.Json2ObjectHandler;
import com.hxexp.weixin.common.json.JsonUtil;
import com.hxexp.weixin.hpe.api.orderdetail.OrderDetailAPI;
import com.hxexp.weixin.hpe.api.orderdetail.model.OrderDetailParamBean;
import com.hxexp.weixin.hpe.api.orderdetail.model.OrderDetailResultBean;

/**
 * 订单详情接口实现类
 *
 * @author li.jiang
 *
 */
@Component
public final class OrderDetailAPIImpl implements OrderDetailAPI {
    private final CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(new PoolingHttpClientConnectionManager()).build();
    private static final String URI = "order/detail";
    @Autowired
    private HpeApiURLConfig config;
    /* (non-Javadoc)
     * @see com.hxexp.weixin.hpe.api.orderdetail.OrderDetailAPI#getOrderDetail(com.hxexp.weixin.model.hpe.orderdetail.OrderDetailParamBean)
     */
    @Override
    public OrderDetailResultBean getOrderDetail(OrderDetailParamBean orderDetailParamBean) throws APIException {
        HttpPost post = new HttpPost(config.getURL(URI));
        String postData = JsonUtil.writeObject2JsonString(orderDetailParamBean);

        // 设置请求体数据
        StringEntity entity = new StringEntity(postData, ContentType.create("application/json", "utf-8"));
        post.setEntity(entity);

        TypeReference<OrderDetailResultBean> typeReference = new TypeReference<OrderDetailResultBean>() {
        };
        try {
            return httpclient.execute(post, new Json2ObjectHandler<OrderDetailResultBean>(typeReference));
        } catch (IOException e) {
            throw new APIException(e);
        }
    }

}
